---------------------------------------
-- The price coefficient for a new rune
---------------------------------------
-- Change this to the desired value. It must be an integer!!!
local RUNE_EXCHANGE_RATE = 2

local success = false

local rune_exchange_choice = 0

-- Init new amount of runes
local rune_might_count_new = 0
local rune_mind_count_new = 0
local rune_magic_count_new = 0

-----------------------
-- Generate object hint
-----------------------
function get_rune_exchange_rate()
	local label = tostring(RUNE_EXCHANGE_RATE)
	return label
end

function get_rune_exchange_count()
	local rune_might = get_rune_count_might()
	local rune_mind = get_rune_count_mind()
	local rune_magic = get_rune_count_magic()
	return "<align=center><label=rune_might><image=Quests_icon_rune_might.png>=<color=225,0,0>"..rune_might.."</color><br><label=rune_mind><image=Quests_icon_rune_mind.png>=<color=225,0,0>"..rune_mind.."</color><br><label=rune_magic><image=Quests_icon_rune_magic.png>=<color=225,0,0>"..rune_magic.."</color>"
end

function get_rune_exchange_exit1()
	if get_rune_count_might() <RUNE_EXCHANGE_RATE then
		return "<label=might_not_enough>"
	else
	    return "<label=dlg_exit>"
	end
end

function get_rune_exchange_exit2()
	if get_rune_count_mind() <RUNE_EXCHANGE_RATE then
		return "<label=mind_not_enough>"
	else
	    return "<label=dlg_exit>"
	end
end

function get_rune_exchange_exit3()
	if get_rune_count_magic() <RUNE_EXCHANGE_RATE then
		return "<label=magic_not_enough>"
	else
	    return "<label=dlg_exit>"
	end

end

function get_rune_exchange_type1()
    if rune_exchange_choice == 1 or rune_exchange_choice==2 then
        return "<image=Quests_icon_rune_might.png>".."<label=rune_might>"
	elseif rune_exchange_choice == 3 or rune_exchange_choice==4 then
	    return "<image=Quests_icon_rune_mind.png>".."<label=rune_mind>"
	elseif rune_exchange_choice == 5 or rune_exchange_choice==6 then
	    return "<image=Quests_icon_rune_magic.png>".."<label=rune_magic>"
	end
end

function get_rune_exchange_type2()
    if rune_exchange_choice == 1 or rune_exchange_choice==6 then
        return "<image=Quests_icon_rune_mind.png>".."<label=rune_mind>"
	elseif rune_exchange_choice == 2 or rune_exchange_choice==4 then
	    return "<image=Quests_icon_rune_magic.png>".."<label=rune_magic>"
	elseif rune_exchange_choice == 3 or rune_exchange_choice==5 then
	    return "<image=Quests_icon_rune_might.png>".."<label=rune_might>"
	end
end
------------------------------
-- Exchange the selected runes
------------------------------
function rune_exchange_change_chosen_runes(choice_number)
	success = false
	-- Sanity check
	if choice_number >= 1 and choice_number <= 6 then

		-- Get the number of runes
		local rune_might_count = get_rune_count_might()
		local rune_mind_count = get_rune_count_mind()
		local rune_magic_count = get_rune_count_magic()

		-- Project the number of runes after the deal (add and delete, respectively)
		local rune_might_add = rune_might_count + 1
		local rune_mind_add = rune_mind_count + 1
		local rune_magic_add = rune_magic_count + 1
		local rune_might_del = rune_might_count - RUNE_EXCHANGE_RATE
		local rune_mind_del = rune_mind_count - RUNE_EXCHANGE_RATE
		local rune_magic_del = rune_magic_count - RUNE_EXCHANGE_RATE

		-- Check what the user's choice was and set up the appropriate number of runes
		if choice_number == 1 and rune_might_count >=RUNE_EXCHANGE_RATE then
			-- Exchange Might for Mind
			rune_might_count_new = rune_might_del
			rune_mind_count_new = rune_mind_add
			rune_magic_count_new = rune_magic_count
			success=true
		elseif choice_number == 2 and rune_might_count >=RUNE_EXCHANGE_RATE then
			-- Exchange Might for Magic
			rune_might_count_new = rune_might_del
			rune_mind_count_new = rune_mind_count
			rune_magic_count_new = rune_magic_add
			success=true
		elseif choice_number == 3 and rune_mind_count >=RUNE_EXCHANGE_RATE then
			-- Exchange Mind for Might
			rune_might_count_new = rune_might_add
			rune_mind_count_new = rune_mind_del
			rune_magic_count_new = rune_magic_count
			success=true
		elseif choice_number == 4 and rune_mind_count >=RUNE_EXCHANGE_RATE then
			-- Exchange Mind for Magic
			rune_might_count_new = rune_might_count
			rune_mind_count_new = rune_mind_del
			rune_magic_count_new = rune_magic_add
			success=true
		elseif choice_number == 5 and rune_magic_count >=RUNE_EXCHANGE_RATE then
			-- Exchange Magic for Might
			rune_might_count_new = rune_might_add
			rune_mind_count_new = rune_mind_count
			rune_magic_count_new = rune_magic_del
			success=true
		elseif choice_number == 6 and rune_magic_count >=RUNE_EXCHANGE_RATE then
			-- Exchange Magic for Mind
			rune_might_count_new = rune_might_count
			rune_mind_count_new = rune_mind_add
			rune_magic_count_new = rune_magic_del
			success=true
		end
	end
end

-----------------------------------------------
-- Conditions for displaying the dialog options
-----------------------------------------------
function rune_exchange_dlg_condition_n(n)
	local success=false
	Logic.hero_lu_var("rune_exchange_dlg",0)
    if n==1 or n==2 then
		if get_rune_count_might() >= RUNE_EXCHANGE_RATE then
			success=true
		end
	elseif n==3 or n==4 then
		if get_rune_count_mind() >= RUNE_EXCHANGE_RATE then
			success=true
		end
	elseif n==5 or n==6 then
		if get_rune_count_magic() >= RUNE_EXCHANGE_RATE then
			success=true
		end
	end
	if success then
	   Logic.hero_lu_var("rune_exchange_dlg",n)
	end
	return success
end

function rune_exchange_dlg_condition1(param) --ftag:if
	return rune_exchange_dlg_condition_n(1)
end

function rune_exchange_dlg_condition2(param) --ftag:if
	return rune_exchange_dlg_condition_n(2)
end

function rune_exchange_dlg_condition3(param) --ftag:if
	return rune_exchange_dlg_condition_n(3)
end

function rune_exchange_dlg_condition4(param) --ftag:if
	return rune_exchange_dlg_condition_n(4)
end

function rune_exchange_dlg_condition5(param) --ftag:if
	return rune_exchange_dlg_condition_n(5)
end

function rune_exchange_dlg_condition6(param) --ftag:if
	return rune_exchange_dlg_condition_n(6)
end

--------------------------------------------------------
-- Store the user's choice in the dialog and force usage
--------------------------------------------------------

function rune_exchange_dlg_use_x(x)
	rune_exchange_change_chosen_runes(x)
	return true
end

function rune_exchange_dlg_use1(param) --ftag:action
    rune_exchange_choice = 1
	return rune_exchange_dlg_use_x(1)
end

function rune_exchange_dlg_use2(param) --ftag:action
    rune_exchange_choice = 2
	return rune_exchange_dlg_use_x(2)
end

function rune_exchange_dlg_use3(param) --ftag:action
    rune_exchange_choice = 3
	return rune_exchange_dlg_use_x(3)
end

function rune_exchange_dlg_use4(param) --ftag:action
    rune_exchange_choice = 4
	return rune_exchange_dlg_use_x(4)
end

function rune_exchange_dlg_use5(param) --ftag:action
    rune_exchange_choice = 5
	return rune_exchange_dlg_use_x(5)
end

function rune_exchange_dlg_use6(param) --ftag:action
    rune_exchange_choice = 6
	return rune_exchange_dlg_use_x(6)
end

function rune_exchange_dlg_choice(param)
	if success then
		Logic.hero_lu_item("rune_might", "count", rune_might_count_new)
		Logic.hero_lu_item("rune_mind", "count", rune_mind_count_new)
		Logic.hero_lu_item("rune_magic", "count", rune_magic_count_new)
	end
	return true
end
